import { Component, OnInit } from "@angular/core";
import { NzNotificationService } from "ng-zorro-antd";
import { Router } from "@angular/router";
import { HttpServcie } from "../../service/http.service";
import { Config } from "../../service/config";
import { AppraisalBean } from "./bean/complaintHis.bean";
import { el } from "@angular/platform-browser/testing/src/browser_util";
import { NzModalService, NzModalSubject } from "ng-zorro-antd";
import * as moment from "moment";

@Component({
  selector: "complaintHis-page",
  templateUrl: "complaintHis.html",
  styleUrls: ["complaintHis.scss"]
})
export class ComplaintHis implements OnInit {
  constructor(
    private httpServcie: HttpServcie,
    private router: Router,
    private _notification: NzNotificationService,
    private nsub: NzModalSubject,
    private modalService: NzModalService
  ) {}

  appraisalBean: AppraisalBean = new AppraisalBean();
  //返回的集合
  list: any = [];
  //总页数
  total: string = "0";
  //多选框
  allChecked: any = false;
  //多选框
  indeterminate: any = false;
  //设置add的
  isAddVisible = false;
  isAddConfirmLoading = false;
  inValue: string;
  currentModal: NzModalSubject;
  feedBackContent: string;
  id: string;
  elevatorNum: string;
  isConfirmLoading = false;
  isFeedBack: string;
  evaluateType: string;
  updateData: any = {};
  modal: any = 0;
  totalList: any = [];
  searchOptions: any = [];
  sureDel = false;
  picList: Array<string>;
  partyType :　string;

  _startDate = null;
  _endDate = null;
  newArray = len => {
    const result = [];
    for (let i = 0; i < len; i++) {
      result.push(i);
    }
    return result;
  };
  _startValueChange = () => {
    if (this._startDate > this._endDate) {
      this._endDate = null;
    }
  };
  _endValueChange = () => {
    if (this._startDate > this._endDate) {
      this._startDate = null;
    }
  };
  _disabledStartDate = startValue => {
    if (!startValue || !this._endDate) {
      return false;
    }
    return startValue.getTime() >= this._endDate.getTime();
  };
  _disabledEndDate = endValue => {
    if (!endValue || !this._startDate) {
      return false;
    }
    return endValue.getTime() <= this._startDate.getTime();
  };
  get _isSameDay() {
    return (
      this._startDate &&
      this._endDate &&
      moment(this._startDate).isSame(this._endDate, "day")
    );
  }
  get _endTime() {
    return {
      nzHideDisabledOptions: true,
      nzDisabledHours: () => {
        return this._isSameDay ? this.newArray(this._startDate.getHours()) : [];
      },
      nzDisabledMinutes: h => {
        if (this._isSameDay && h === this._startDate.getHours()) {
          return this.newArray(this._startDate.getMinutes());
        }
        return [];
      },
      nzDisabledSeconds: (h, m) => {
        if (
          this._isSameDay &&
          h === this._startDate.getHours() &&
          m === this._startDate.getMinutes()
        ) {
          return this.newArray(this._startDate.getSeconds());
        }
        return [];
      }
    };
  }

  ngOnInit() {
    this.appraisalBean.pageSize = 10;
    this.appraisalBean.currentPage = 1;

    this.partyType = JSON.parse(window.localStorage["user"]).partyType;
    console.log(this.partyType);
    if(this.partyType == "1"){
      this.appraisalBean.maintenancePartyId = JSON.parse(window.localStorage["user"]).partyId;
    }
    if(this.partyType == "2"){
      this.appraisalBean.usedPartyId = JSON.parse(window.localStorage["user"]).partyId;
    }
    this.query();
  }

  //查询
  query() {
    // if (this.isFeedBack) {
    this.appraisalBean.isFeedBack = this.isFeedBack;
    // }
    // if (this.evaluateType) {
    this.appraisalBean.evaluateType = this.evaluateType;
    // }
    if (this.elevatorNum) {
      this.appraisalBean.elevatorNum = this.elevatorNum;
    }else{
      this.appraisalBean.elevatorNum = null;
    }

    this.httpServcie.getElevatorAppraisalByParty(this.appraisalBean).subscribe(
      data => {
        if (data.status === 200) {
          this.list = data.data.list;
          this.total = data.data.total;
        } else if (data.status === 401) {
          this.router.navigateByUrl("/login");
          return;
        } else {
          // 异常
          this._notification.create("error", "错误提示", data.message);
        }
      },
      error => {
        console.log(JSON.stringify(error));
      }
    );
  }

  // 查询面板
  panels = {
    active: true,
    name: "查询",
    disabled: false,
    childPanel: [
      {
        active: false,
        name: "查询"
      }
    ]
  };

  //table加载数据时
  displayDataChange($event) {
    this.list = $event;
    this.refreshStatus();
  }

  //重置选择框
  refreshStatus() {
    const allChecked = this.list.every(
      value => value.disabled || value.checked
    );
    const allUnChecked = this.list.every(
      value => value.disabled || !value.checked
    );
    this.allChecked = allChecked;
    this.indeterminate = !allChecked && !allUnChecked;
  }

  //全选
  checkAll(value) {
    if (value) {
      this.list.forEach(data => {
        if (!data.disabled) {
          data.checked = true;
        }
      });
    } else {
      this.list.forEach(data => (data.checked = false));
    }
  }

  //分页
  nzPageSizeChange(event) {
    this.appraisalBean.pageSize = event;
    this.appraisalBean.currentPage = 1;
    this.query();
  }

  //输入框的方法
  nzPageIndexChange(event) {
    this.appraisalBean.currentPage = event;
    this.query();
  }

  //当前页
  nzPageIndexClickChange(event) {
    this.appraisalBean.currentPage = event;
    this.query();
  }

  //弹出详情
  detailNotification(data, type) {
    if (type == 1) {
      this._notification.blank("投诉详情", data.evaluateMsg);
    }
    if (type == 2) {
      this._notification.blank("回复详情", data.feedBackContent);
    }

    //确认已读，修改状态
    //data.isRead = '1';
    // let params = {
    //   isRead:'1',
    //   id: data.id
    // }
    // let ob = this.httpServcie.updateReadState(params);
    // ob.subscribe((data)=>{
    //   console.log(data);
    // });
  }

  showImage(titlepic, contentpic, footerpic, param) {
    this.id = param.id;
    this.currentModal = this.modalService.open({
      title: titlepic,
      content: contentpic,
      footer: footerpic,
      maskClosable: false,
      width: 800,
      style: {
        top: "20px"
      },
      onOk() {
        console.log("Click ok");
      }
    });
  }

  feedBack() {
    let params = {
      id: this.id,
      isFeedBack: "1",
      feedBackContent: this.feedBackContent,
      userId:JSON.parse(window.localStorage["user"]).id,
      roleId:JSON.parse(window.localStorage["user"]).roleId
    };
    this.httpServcie.updateFeedBack(params).subscribe(data => {
      this.currentModal.destroy();
      this.feedBackContent = "";
      this.query();
    });
  }
}
